package com.itheima.controller;

import com.itheima.pojo.Article;
import com.itheima.pojo.Category;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    /**
     * 文章服务类
     */
    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     *
     * @param article 文章对象
     * @return 结果对象
     */
    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    /**
     * 获取文章列表
     *
     * @param pageNum    页码
     * @param pageSize   每页数量
     * @param categoryId 分类ID
     * @param state      文章状态
     * @return 分页对象
     */
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false)
            Integer categoryId,
            @RequestParam(required = false)
            String state
    ) {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }


    //获取文章详细
    @GetMapping("/{id}")
    public Result<Article> get(@PathVariable("id") Integer id) {
        Article article = articleService.get(id);
        return Result.success(article);
    }

    //更新文章
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article){
        articleService.update(article);
        return Result.success();
    }

    //删除文章
    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }

}

